package com.block.datapush.common.filter.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.common.service.LogFactory;
import com.block.datapush.common.enums.FilterErrorEnum;
import com.block.datapush.common.filter.IFilterHandlerService;
import com.block.datapush.common.model.param.FilterHandlerParam;
import com.block.datapush.common.model.result.FilterHandlerResult;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class FilterAgeHandlerService implements IFilterHandlerService {

    private static Logger log = LogFactory.getPushLog();

    @Override
    public FilterHandlerResult handlerFilterLogic(FilterHandlerParam handlerParam, MemberExtInfo memberExtInfo) {

        Integer minAge = ObjectUtil.isEmpty(handlerParam.getMinAge())?20:handlerParam.getMinAge();
        Integer maxAge = ObjectUtil.isEmpty(handlerParam.getMaxAge())?60:handlerParam.getMaxAge();

        Integer curAge = memberExtInfo.getAge();

        if (curAge >= minAge && curAge <= maxAge){
            return FilterHandlerResult.success();
        }

        String message = StrUtil.format("【filter】{}-{}【value】{}", minAge, maxAge, curAge);
        log.info("【机构条件判断】false【年龄】{}", message);
        return FilterHandlerResult.error(FilterErrorEnum.AGE_ERROR, message);
    }
}
